package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import Entities.ForgotPasswordEntity;
import Entities.WindowClose;
import GUI.ForgotPasswordGUI;
import Server.RsWraper;
import Server.Tuple;

/**
 * The Class ForgotPasswordController.
 * Controller for receiving forgotten password activity.
 */
public class ForgotPasswordController implements Observer {
	
	/** The forgot password entity. */
	private ForgotPasswordEntity forgotPasswordEntity;
	
	/** The forgot password gui. */
	private ForgotPasswordGUI forgotPasswordGUI;
	
	/** The previous controller. */
	private LoginController prev;
	
	/** The tuples. */
	private ArrayList<Tuple> tuples;

	/**
	 * Instantiates a new forgot password controller.
	 *
	 * @param forgotPasswordGUI the forgot password gui
	 * @param forgotPasswordEntity the forgot password entity
	 */
	public ForgotPasswordController(ForgotPasswordGUI forgotPasswordGUI,ForgotPasswordEntity forgotPasswordEntity) {
		this.forgotPasswordEntity = forgotPasswordEntity;
		this.forgotPasswordGUI = forgotPasswordGUI;
		ClientController.setObserver(this);
		this.forgotPasswordGUI.addWindowListener(new WindowClose());
		this.forgotPasswordGUI.addOkActionListener(new ForgotPassword());
		this.forgotPasswordGUI.addBackActionListener(new BackListener());
		this.forgotPasswordGUI.setVisible(true);
	}






/**
 * The Class ForgotPassword.
 */
class ForgotPassword implements ActionListener
{
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String id=forgotPasswordGUI.getIdField();
		String query = "SELECT users.password FROM users WHERE users.uid= '"+id+"'";		//retrieve password query.
		try {
			ClientController.observableClient.sendToServer(query);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}

/**
 * The listener interface for receiving back events.
 * The class that is interested in processing a back
 * event implements this interface, and the object created
 * with that class is registered with a component using the
 * component's <code>addBackListener<code> method. When
 * the back event occurs, that object's appropriate
 * method is invoked.
 *
 * @see BackEvent
 */
class BackListener implements ActionListener
{

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		forgotPasswordGUI.dispose();
		prev.getLoginGui().setVisible(true);
		ClientController.setObserver(prev);
	}

}

/**
 * Sets the prev.
 *
 * @param prev the new prev
 */
public void setPrev(LoginController prev) {
	this.prev = prev;
}

/* (non-Javadoc)
 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
 */
@Override
public void update(Observable o, Object arg) {

	
if(arg.toString().equals("empty query"))
	forgotPasswordGUI.displayWarningMessage("This user is not exists");
else{		
	tuples=((RsWraper) arg).getRows();
	forgotPasswordEntity.setPassword(tuples.get(0).get("PASSWORD"));
	forgotPasswordGUI.displayMessage(forgotPasswordEntity.getPassword());
	
}
}
}